package com.ebeitech.doorapp.http.service;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.ebeitech.doorapp.R;
import com.ebeitech.doorapp.http.EbeiErrorCode;
import com.ebeitech.doorapp.http.HttpService;
import com.ebeitech.doorapp.http.model.BaseModel;
import com.ebeitech.doorapp.view.BaseActivity;
import com.ebeitech.library.ui.dialog.CommonAlertDialogFragment;
import com.ebeitech.library.util.StringUtil;
import com.ebeitech.library.util.ViewUtil;

import org.json.JSONObject;

import rx.Subscriber;

/**
 * Created by alice on 2017/11/1.
 */
public class BaseSubscriber<T> extends Subscriber<T> {

	private Context mContext;
	private Activity mActivity;
	private BaseActivity mBaseActivity;

	private T t;
	private CommonAlertDialogFragment mProgressDialog;
	public boolean isProgressDialogVisible = true;

	private Handler mHandler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 0: {
					if (isProgressDialogVisible && mActivity != null && !mActivity.isFinishing()) {
						if (mBaseActivity != null && mBaseActivity.isOnPause()) {
							mProgressDialog = null;
							break;
						}
						mProgressDialog = ViewUtil.showProgressDialog(mContext, -1, R.string.ebei_loading);
					}
					break;
				}
				case 1: {
					if (isProgressDialogVisible) {
						ViewUtil.dismissDialog(mProgressDialog);
					}
					break;
				}
				case -1: {
					if (isProgressDialogVisible) {
						ViewUtil.toastMsg(mContext, R.string.ebei_network_load_failure);
					}
					onError(new EbeiErrorCode(EbeiErrorCode.ERROR_CODE_NET_ERROR));
					break;
				}
				case -2: {
					if (isProgressDialogVisible) {
						ViewUtil.toastMsg(mContext, msg.obj.toString());
					}
					EbeiErrorCode errorCode = new EbeiErrorCode();
					errorCode.setErrorMsg(msg.obj.toString());
					onError(errorCode);
					break;
				}
				case -3: {
					EbeiErrorCode errorCode = new EbeiErrorCode(EbeiErrorCode.ERROR_CODE_CODE_FAILED);
					errorCode.setResult(msg.obj);
					onError(errorCode);
					break;
				}
			}
		}
	};

	public BaseSubscriber(Context context) {
		this.mContext = context;
		if (mContext instanceof Activity) {
			mActivity = (Activity) mContext;
			if (mActivity instanceof BaseActivity) {
				mBaseActivity = (BaseActivity) mActivity;
			}
		}
	}

	public void setIsProgressDialogVisible(boolean isProgressDialogVisible) {
		this.isProgressDialogVisible = isProgressDialogVisible;
	}

	@Override
	public void onStart() {
		super.onStart();
		mHandler.sendEmptyMessage(0);
	}

	@Override
	public void onError(Throwable e) {
		EbeiErrorCode errorCode = null;
		try {
			String result = e.getMessage();
			if (!StringUtil.isStringNullOrEmpty(result) && result.contains("ErrorCode")) {
				JSONObject jsonObject = new JSONObject(result);
				errorCode = new EbeiErrorCode();
				int code = Integer.valueOf(jsonObject.optString("ErrorCode"));
				errorCode.setErrorCode(code);
				errorCode.setErrorMsg(jsonObject.optString("ErrorMsg"));

				mHandler.sendEmptyMessage(1);
				onError(errorCode);
				return;
			}
		} catch (Exception e1) {}

		mHandler.sendEmptyMessage(-1);
		mHandler.sendEmptyMessage(1);
	}

	@Override
	public void onCompleted() {
		mHandler.sendEmptyMessage(1);
		if (t != null) {
			onLoadData(t);
		}
	}

	@Override
	public void onNext(T t) {
		if (t == null) {
			mHandler.sendEmptyMessage(-1);
			onError(new EbeiErrorCode(EbeiErrorCode.ERROR_CODE_NET_ERROR));
			return;
		}

		if (t instanceof BaseModel) {
			BaseModel baseModel = (BaseModel) t;
			if ((!StringUtil.isStringNullOrEmpty(baseModel.getRetCode()) && !HttpService.RESULT_CODE_SUCCESS.equals(baseModel.getRetCode())) || !StringUtil.isStringNullOrEmpty(baseModel.getError_description())) {
				if (!StringUtil.isStringNullOrEmpty(baseModel.getRetMsg())) {
					Message message = Message.obtain();
					message.what = -2;
					message.obj = baseModel.getRetMsg();
					mHandler.sendMessage(message);
				} else {
					Message message = Message.obtain();
					message.what = -3;
					if (!StringUtil.isStringNullOrEmpty(baseModel.getError_description())) {
						message.obj = baseModel.getError_description();
					} else {
						message.obj = t;
					}
					mHandler.sendMessage(message);
				}
				return;
			}
		}

		this.t = t;
	}

	public void onLoadData(T t) {

	}

	public void onError(EbeiErrorCode errorCode) {

	}

}
